#!/bin/bash
#
# live: Init script for eeedora - live and non-live
#
# chkconfig: 345 00 99
# description: Init script for live image.

liveuser="eeedora"

# This has already been unwrapped (in the ISO kickstart code)
setup=/root/eee-setup

ShowProgress() {
 echo $"eeedora-init :"`date +%F_%H-%M`" : $1" >> /etc/eeedora.progress
}

DoNotStart() {
 if [ -f /etc/init.d/$1 ]; then chkconfig --level 345 $1 off; fi
}

ShowProgress "Loading /etc/init.d/functions"
. /etc/init.d/functions

exists() {
 which $1 >/dev/null 2>&1 || return
 $*
}

# This tests whether we're repeating something we've already done.
configured=/.eeedora-configured

#ShowProgress "Test whether this has been run - if so, abort"
if [ -e  ${configured} ] ; then
 ShowProgress "Apparently this has already happened - abort"
 exit 0
fi

ShowProgress "Touching ${configured}"
touch ${configured}

# Ok, so this is certainly the first time run here # Is this is a live install ?

#if ! strstr "`cat /proc/cmdline`" liveimg || [ "$1" != "start" ] || [ -e /.liveimg-configured ] ; then

live=false
ShowProgress "Testing cmdline - are we a live image ?"
# This is the kernel command line - a pretty good indicator
if strstr "`cat /proc/cmdline`" liveimg ; then
 ShowProgress "Apparently this is a live image"
 live=true
fi

#if ! ${live}; then 
# ShowProgress "Installing the atl2 kernel module - again..."
# ${setup}/atl2/install-atl2 ${setup}
#fi

#ShowProgress "TODO : Fix up /etc/fstab (not done)"
# Use sed to fix up /etc/fstab (like the gdm.conf below)

if ${live}; then 
	ShowProgress "Try to Mount live image"
	if [ -b /dev/live ]; then
 	ShowProgress "Actually Mount live image"
		mkdir -p /mnt/live
		mount -o ro /dev/live /mnt/live
	fi
fi

# TODO :
ShowProgress "Fix up fstab to log to RAM"
# Adding the following to /etc/fstab        tmpfs /var/log tmpfs defaults,noatime 0 0
# Repeat for /tmp, /var/lock, /var/run and /var/tmp
#for i in /var/log /tmp /var/lock /var/run /var/tmp; do 
#	echo "tmpfs $i tmpfs defaults,noatime 0 0" >> /etc/fstab
#done
for i in /var/log /tmp; do 
	echo "tmpfs $i tmpfs defaults,noatime 0 0" >> /etc/fstab
#	echo "tmpfs $i tmpfs defaults	0 0" >> /etc/fstab
done

#if ! [ 1 ]; then # DISABLE CREATION OF SWAP PARTITION - SAVE FLASH RAM OVERWRITES
# ShowProgress "Enable swaps unless requested otherwise"
# # This is the list of current swap partitions
# swaps=`blkid -t TYPE=swap -o device`
# if ! strstr "`cat /proc/cmdline`" noswap -a [ -n "$swaps" ] ; then
#  # Go through the list until we get a hit...
#  for s in $swaps ; do
#    action "Enabling swap partition $s" swapon $s
#  done
# fi
#else
# ShowProgress "Eee has swap disabled"
#fi

# TODO : cpu scaling : This would increase battery time
# echo ondemand > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
# Default state for Eee :
# echo performance > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor


#ShowProgress "Configure X, allowing user to override xdriver"
#for o in `cat /proc/cmdline` ; do
#	case $o in
#	xdriver=*)
#		xdriver="--set-driver=${o#xdriver=}"
#		;;
#	esac
#done

#ShowProgress "system-config-display ($xdriver)"
#exists system-config-display --noui --reconfig --set-depth=24 $xdriver

ShowProgress "Unmute sound card"
#exists amixer set iSpeaker unmute > /dev/null
exists amixer set Master unmute > /dev/null
#exists alsaunmute 0 2> /dev/null

if ${live}; then 
 ShowProgress "Turn off firstboot"
 echo "RUN_FIRSTBOOT=NO" > /etc/sysconfig/firstboot
else 
 ShowProgress "Turn off firstboot"
 echo "RUN_FIRSTBOOT=NO" > /etc/sysconfig/firstboot
fi

ShowProgress "Add useful repos"
cp ${setup}/repos/atrpms.repo /etc/yum.repos.d/
rpm --import ${setup}/repos/RPM-GPG-KEY.atrpms

rpm -ivh ${setup}/repos/livna-release-9.rpm
rpm --import ${setup}/repos/RPM-LIVNA-GPG-KEY

ShowProgress "Don't start yum-updatesd"
chkconfig --level 345 yum-updatesd off

ShowProgress "Don't start cron/at as they tend to be painful in a Live context"
# don't start cron/at as they tend to spawn things which are
# disk intensive that are painful on a live image
if ${live}; then 
 DoNotStart "crond"
 DoNotStart "atd"
 DoNotStart "anacron"
 DoNotStart "readahead_early"
 DoNotStart "readahead_later"
# chkconfig --level 345 readahead_later off
fi
# This one screws up wifi-radar and wicd - which is/are better at managing the wifi
ShowProgress "Don't start NetworkManager"
DoNotStart "NetworkManager"

ShowProgress "Stopgap fix for RH #217966; should be fixed in HAL instead"
touch /media/.hal-mtab

ShowProgress "Copy over known-good xorg.conf"
cp ${setup}/xorg/xorg.conf /etc/X11/

ShowProgress "Create /etc/sysconfig/desktop (needed for installation)"
cp ${setup}/xfce/sysconfig-desktop /etc/sysconfig/desktop 

# disable screensaver locking
ShowProgress "Disable screensaver locking"
gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
            -s -t bool /apps/gnome-screensaver/lock_enabled false >/dev/null

ShowProgress "Fix sound devices for pulseaudio"
echo "chmod -R a+rwx /dev/snd" >> /etc/rc.local 

ShowProgress "Create path for wicd to write log to"
mkdir -p /var/log/wicd
# Hopefully this is before wicd tries to start up for the first time



#
# Now do all the stuff for the special new eeedora user
#

# This group is going to be able to plug and unplug devices

ShowProgress "Add '${liveuser}' user with no passwd"
useradd -c "Eeedora Live" ${liveuser} 
passwd -d ${liveuser} > /dev/null

# In case this group doesn't exist already...
groupadd --system fuse 

# Enable this user to use 'fuse' (for encfs, etc)
usermod -a -G fuse ${liveuser} 

livehome=/home/${liveuser}

ShowProgress "Process display manager options"
${setup}/xdm/setup-display-manager ${setup} ${liveuser}

#ShowProgress "Set up XFCE for '${liveuser}'"
#${setup}/xfce/setup-xfce ${setup} ${liveuser}

# Trying to do without...
#ShowProgress "Start up pulseaudio for '${liveuser}'"
#${setup}/misc/fix-pulseaudio ${setup} ${liveuser}

ShowProgress "Fix up truecrypt sudo for '${liveuser}'"
${setup}/truecrypt/sudo-truecrypt-fix ${setup} ${liveuser}

#ShowProgress "Set up various defaults for SciTE for '${liveuser}'"
#cat ${setup}/misc/scite.monospaced >> ${livehome}/.SciTEUser.properties
#cat ${setup}/misc/scite.filesearch >> ${livehome}/.SciTEUser.properties
#cat ${setup}/misc/scite.tabs >> ${livehome}/.SciTEUser.properties

# Only do this for the live user - since it only makes sense to have a liveinstall icon on that desktop
if ${live}; then 
# ShowProgress "Fix up the /usr/sbin/liveinst script - it needs to accept command line params"
# ${setup}/live-install/fix-liveinst-script ${setup}

 # ShowProgress "Add a desktop link to install the live image for '${liveuser}'"
 # ${setup}/live-install/fix-sudo-mount ${setup}

 ShowProgress "Add a desktop link to install the live image for '${liveuser}'"
 mkdir /home/${liveuser}/Desktop
 # This script calls /usr/bin/liveinst, which gets the root password before invoking /usr/sbin/liveinst
 cp ${setup}/live-install/eee-live-install ${livehome}/Desktop/Install-Live-Image
fi

#ShowProgress "Set up firefox for '${liveuser}' (this is a personal thing)"
#mkdir ${livehome}/.mozilla
#tar -xzf ${setup}/misc/firefox.tar.gz -C ${livehome}/
# TODO : 
# Installed the ‘Littlefox’ theme into FireFox (takes up much less screen real-estate)

# ShowProgress "Allow '${liveuser}' to automount USB plugin drives"
# ${setup}/services/automount-volumes_hal-fix ${setup} ${liveuser}

ShowProgress "Set up the hotkey App for '${liveuser}'"
app=${livehome}/.hotkey-app
cp ${setup}/acpi/dot-hotkey-app ${app}
chmod 755 ${app}




# Do this permission setting last - 

#ShowProgress "Set User permissions - Can be removed once the ui hacks have gone"
#chown -R ${liveuser}:${liveuser} ${livehome}/
#chmod 700 ${livehome}/

ShowProgress "/sbin/restorecon for ${liveuser} home directory"
/sbin/restorecon -R ${livehome}/
chmod 700 ${livehome}/

#more /etc/eeedora.progress
#read enter

# Enable this line if the login process has problems : 
#   it allows command line interaction before any services start up...
#if ${live}; then
# ${setup}/misc/insecure-prelogin-shell.bash
#fi

# Finished
